package nil.ed.simulator.request.ui.jfx.controller;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import lombok.Getter;
import lombok.Setter;
import nil.ed.simulator.request.enums.events.EventMode;
import nil.ed.simulator.request.enums.events.EventType;
import nil.ed.simulator.request.event.Event;
import nil.ed.simulator.request.event.EventSource;
import nil.ed.simulator.request.event.bus.EventBus;
import nil.ed.simulator.request.guice.GuiceManager;
import nil.ed.simulator.request.ui.jfx.manager.ControllerManager;
import nil.ed.simulator.request.ui.jfx.ui.Controller;
import nil.ed.simulator.request.ui.jfx.ui.table.TableModelContext;

import java.net.URL;
import java.util.ResourceBundle;

@SuppressWarnings("rawtypes")
public class TableViewController extends TableView implements Initializable, Controller, EventSource {

	@FXML
	@Getter
	private TableView<Object> tableView;
	@Setter
	@Getter
	private ControllerManager controllerManager;
	@Getter
	private final ObservableList<Object> dataList = FXCollections.observableArrayList();
	@Getter
	private TableModelContext modelContext;

	@Override
	public void initialize(URL url, ResourceBundle source) {
		ControllerManager.addController(this);
		this.resetTable(Void.class);
		tableView.setItems(dataList);
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	public void resetTable(Class<?> clazz) {
		this.modelContext = new TableModelContext(clazz);
		Platform.runLater(() -> {
			tableView.getColumns().clear();
			this.modelContext.getColumnConfigs().forEach(columnConfig -> {
				TableColumn tableColumn = new TableColumn(columnConfig.getTitle());
				tableColumn.setMinWidth(columnConfig.getMinWidth() == -1 ? 150 : columnConfig.getMinWidth());
				tableColumn.setMinWidth(columnConfig.getMinHeight() == -1 ? 150 : columnConfig.getMinHeight());
				tableColumn.setCellValueFactory(new PropertyValueFactory<>(columnConfig.getField().getName()));
				tableView.getColumns().add(tableColumn);
			});
			if (this.modelContext.getTableCallback() != null) {
				this.modelContext.getTableCallback().accept(tableView);
			}
			tableView.refresh();
		});

	}

	public void onDragOver(DragEvent e) {
		e.acceptTransferModes(TransferMode.MOVE);
	}

	public void onDragDropped(DragEvent e) {
		Dragboard board=e.getDragboard();
		if (!board.hasFiles()) {
			return;
		}

		GuiceManager.INJECTOR.getInstance(EventBus.class).publish(
				Event.builder()
						.eventType(EventType.FILE_IMPORT)
						.eventMode(EventMode.SYNC)
						.data(board.getFiles())
						.source(this)
						.build());
	}

	@Override
	public String getEventSourceId() {
		return tableView.getId();
	}

	@Override
	public String getEventSourceName() {
		return "主表格";
	}
}
